package com.frame.work.frame.deal.device0fbe;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.constant.Const;
import com.frame.work.common.constant.TypeEnum;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.DeviceAssociatedInformation;
import com.frame.work.entity.TblSecurityLog;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.SelectDeviceMapper;
import com.frame.work.service.TblSecurityLogService;
import com.frame.work.service.impl.JpushService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 挂壁式人体红外
 */
@Data
public class GuaBiShiRenTiHongWai_0FBE8602 extends BaseFrame {

    private String switchState;

    public GuaBiShiRenTiHongWai_0FBE8602(String gatewayMac, String data) {

        //将基础数据进行处理
        super(gatewayMac, data);

        //处理此设备特有的数据

        String reportType = StringUtils.substring(data, 40, 42);
        String alarmState = StringUtils.substring(data, data.length() - 8, data.length() - 6);
        String fangChaiState = StringUtils.substring(data, data.length() - 6, data.length() - 4);
        //数据库操作 begin
        String reportNum = "00";
        if (reportNum.equals(reportType)) {
            //获取mapper进行数据库操作
            SelectDeviceMapper testMapper = (SelectDeviceMapper) SpringUtil.getBean(SelectDeviceMapper.class);
            List<DeviceAssociatedInformation> list = testMapper.getDevice1(this.getDeviceMac());
            if (list.size() > 0) {
                DeviceAssociatedInformation relationMessage = list.get(0);
                String pushType = "1";
                String defendStatus = "02";
                String homeName = "";
                String content = "";

                if (relationMessage.getHomeName() != null && relationMessage.getHomeName() != "") {
                    homeName = relationMessage.getHomeName();
                }
                if (relationMessage.getPushType() != null && relationMessage.getPushType() != "") {
                    pushType = relationMessage.getPushType();
                }
                if (relationMessage.getDefendStatus() != null && relationMessage.getDefendStatus() != "") {
                    defendStatus = relationMessage.getDefendStatus();
                }


                String type = null;
                String deviceName = relationMessage.getDeviceName();
                if (deviceName == "" || deviceName == null) {
                    content = homeName + "人体红外";
                    type = TypeEnum.STATE.code();
                } else {
                    content = homeName + deviceName;
                    type = TypeEnum.STATE.code();
                }
                if ("01".equals(alarmState)) {
                    content = content + "检测到有人出现";
                    type = TypeEnum.STATE.code();
                }


                /**
                 * 云片短信
                 */
                /**
                 * 极光推送
                 */

                String orgCode = relationMessage.getOrganizationCode();
                String account = relationMessage.getAccount();
                String nickName = relationMessage.getNickName();


                String userId = relationMessage.getUserId();
                String deviceId = relationMessage.getDeviceId();
                String sourceId = relationMessage.getSourceId();
                String sourceType = relationMessage.getSourceType();

                if (relationMessage.getSourceId() != null && !"".equals(relationMessage.getSourceId())) {
                    JpushService jPushService = (JpushService) SpringUtil.getBean(JpushService.class);
                    try {
                        jPushService.sendNoSave("", content, type, sourceId, Const.MsgType.MSG_WARNING_TYPE, deviceId, userId, sourceType, orgCode);

                    } catch (Exception e) {

                    }
                }


                //保存报警信息
                TblSecurityLog tblSecurityLog = new TblSecurityLog();
                tblSecurityLog.setGatewayMac(this.getGatewayMac());
                tblSecurityLog.setDeviceMac(this.getDeviceMac());
                tblSecurityLog.setContent(content);

                tblSecurityLog.setUserId(userId);
                TblSecurityLogService tblSecurityLogService = (TblSecurityLogService) SpringUtil.getBean(TblSecurityLogService.class);
                tblSecurityLogService.insert(tblSecurityLog);
            }
        }

        //数据库操作 end

        this.setSwitchState(alarmState);

        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("reportType", reportType);
        actions.put("alarmState", alarmState);
        actions.put("fangChaiState", fangChaiState);
        this.setActions(actions);

        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, json);

    }

}
